package com.yc.jiushujie.jsj_user.mapper;

import com.yc.jiushujie.entity.JsjBook;
import com.yc.jiushujie.entity.JsjUser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Select;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 无罪`
 * @since 2022-08-03
 */
public interface JsjUserMapper extends BaseMapper<JsjUser> {

    @Select("select * from jsj_user where account = #{account} ")
    int selectIdByAccount(String account);

    @Select("select * from jsj_user"+
            " where account=#{account}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "books", column = "id",
                    many = @Many(select = "selectbook"))
    })
   JsjUser selectownerbooks(String account);

    @Select("select * from jsj_book where owner_id=#{id}")
    List<JsjBook> selectbook(int id);


    @Select("select * from jsj_book where owner_id= #{id} LIMIT #{page},#{size} ")
        List<JsjBook> getownerbooksBypage(@Param("id") String id,
                                @Param("page") int page,
                                @Param("size") int size);

    @Select("select count(*) from (select * from jsj_book where owner_id= #{id} )a ")
    long getownerbooksBypageTotal(@Param("id") String id);

//    @Select("SELECT\n" +
//            "\t*\n" +
//            "FROM\n" +
//            "\tjsj_book b\n" +
//            "JOIN jsj_user u ON b.owner_id = u.id\n" +
//            "WHERE\n" +
//            "\tb.id = #{bid}")
//    @Result(property = "id",column = "id1",id = true)
//    @Result(property = "name",column = "name1" )
//    List<JsjUser> selectowner(@RequestParam ("bid") int id);
@Select("select * from jsj_user where id=#{id}")
@Results({
        @Result(column = "id",property = "id"),
        @Result(column = "id",property = "books",
        many = @Many(select = "selectBookByCart"))

})
    JsjUser selectById(int id);
@Select("select * from jsj_book b join jsj_cart c on b.id=c.bid"+
" where c.uid=#{id}")
@Results({
        @Result(column = "id",property = "id"),
        @Result(property = "user", column = "id",
                many = @Many(select = "selectBookOwner"))
})
List<JsjBook> selectBookByCart(int id);

@Select(" select *  from  jsj_user u join jsj_book b on u.id=b.owner_id"+
 " where b.id=#{id}")
    JsjUser selectBookOwner(int id);

}
